package com.mybase.app.fourBase;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;
import com.mybase.app.app.HomeActivity;
import com.mybase.app.R;

/**
 * Created by happy_000 on 2014/10/17.
 */
public class NotificationActivity extends Activity implements View.OnClickListener{
/**************
 * 　     1>实例化一个NotificationCompat.Builder对象;如builder
 　　　　 2>调用builder的相关方法对notification进行上面提到的各种设置
 　　　　 3>调用builder.build()方法此方法返回一个notification对象。
 　　　　 4>实例化一个NotificationManager对象；如：manager
 　　　　 5>调用manager的notify方法。

         Notification的属性：
         audioStreamType 当声音响起时，所用的音频流的类型
         contentIntent 当通知条目被点击，就执行这个被设置的Intent.
         contentView 当通知被显示在状态条上的时候，同时这个被设置的视图被显示.
         defaults 指定哪个值要被设置成默认的.
         deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候，这个被设置的Intent被执行.
         icon 状态条所用的图片.
         iconLevel 假如状态条的图片有几个级别，就设置这里.
         ledARGB LED灯的颜色.
         ledOffMS LED关闭时的闪光时间（以毫秒计算）
         ledOnMS LED开始时的闪光时间（以毫秒计算）
         number 这个通知代表事件的号码
         sound 通知的声音
         tickerText 通知被显示在状态条时，所显示的信息
         vibrate 振动模式.
         when 通知的时间戳.
         Notification提供了丰富的手机提示方式：
         a)在状态栏(Status Bar)显示的通知文本提示，如：
         notification.tickerText = "hello";
         b)发出提示音，如：
         notification.defaults = Notification.DEFAULT_SOUND;
         notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
         notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
         c)手机振动，如：
         notification.defaults = Notification.DEFAULT_VIBRATE;
         long[] vibrate = {0,100,200,300};
         notification.vibrate = vibrate;
         d)LED灯闪烁，如：
         notification.defaults = Notification.DEFAULT_LIGHTS;
         notification.ledARGB = 0xff00ff00;
         notification.ledOnMS = 300;
         notification.ledOffMS = 1000;
         notification.flags = Notification.FLAG_SHOW_LIGHTS;
         4）发送通知：
         private static final int ID_NOTIFICATION = 1;
         mNotificationManager.notify(ID_NOTIFICATION, notification);
 *
 */

    Button m_Button1;
    TextView m_txtView;
    NotificationManager m_NotificationManager;
    Notification m_Notification;
    Intent m_Intent;
    PendingIntent m_PendingIntent;
    private Button m_Button2,m_Button3,m_Button4,m_Button5;


    /**
     * Called when the activity is first created.
     */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.natification);
        m_NotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        m_Button1 = (Button) this.findViewById(R.id.natification_Button01);
        m_Button2 = (Button) this.findViewById(R.id.natification_Button02);
        m_Button3 = (Button) this.findViewById(R.id.natification_Button03);
        m_Button4 = (Button) this.findViewById(R.id.natification_Button04);
        m_Button5 = (Button) this.findViewById(R.id.natification_Button05);
        m_Button1.setOnClickListener(this);
        m_Button2.setOnClickListener(this);
        m_Button3.setOnClickListener(this);
        m_Button4.setOnClickListener(this);
        m_Button5.setOnClickListener(this);

        //点击通知时转移内容

        m_Intent = new Intent(this, HomeActivity.class);
        m_PendingIntent = PendingIntent.getActivity(this, 0, m_Intent, 0);

    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.natification_Button01:
                m_Notification = new Notification();
                //设置通知在状态栏显示的图标
                m_Notification.icon = R.drawable.ic_launcher;
                //当我们点击通知时显示的内容
                m_Notification.tickerText = "standard Notification 当我们点击通知时显示的内容,通知内容.....";
                //通知时发出的默认声音
                m_Notification.defaults = Notification.DEFAULT_SOUND;
                //设置通知显示的参数
                m_Notification.setLatestEventInfo(NotificationActivity.this, "standard Notification", "通知显示的参数Button1通知", m_PendingIntent);
                //这个可以理解为开始执行这个通知
                m_NotificationManager.notify(1, m_Notification);
                break;

            case R.id.natification_Button02:
                Notification.Builder builder2=new Notification.Builder(NotificationActivity.this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setTicker("m_Notification Builder通知")//// 当当前的notification被放到状态栏上的时候，提示内容
                        .setWhen(System.currentTimeMillis())//通知的时间戳.
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setContentIntent(m_PendingIntent);
                Notification notification=builder2.getNotification();
                m_NotificationManager.notify(2,notification);
                break;
            case R.id.natification_Button03:
                Notification.Builder builder3=new Notification.Builder(NotificationActivity.this)
                        .setTicker("progress 通知")//// 当当前的notification被放到状态栏上的时候，提示内容
                        .setContentText("progress")
                        .setWhen(System.currentTimeMillis())
                        .setProgress(100,50,false)
                        .setContentIntent(m_PendingIntent)
                        .setDefaults(Notification.DEFAULT_SOUND);

                Notification notification3=builder3.getNotification();
                m_NotificationManager.notify(3,notification3);
                break;
            case R.id.natification_Button04:
                /***
                 * 使用自定义的Notification
                 要创建一个自定义的Notification，可以使用RemoteViews。要定义自己的扩展消息，首先要初始化一个RemoteViews对象，
                 然后将它传递给Notification的contentView字段，再把PendingIntent传递给contentIntent字段。
                 以下示例代码是完整步骤：
                 1>1、创建一个自定义的消息布局 view.xml
                 2>2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
                 3>3、为Notification的contentIntent字段定义一个Intent(注意，使用自定义View不需要setLatestEventInfo()方法)
                 4>4、发送通知
                 */


                //定义Notification的各种属性
                int icon = R.drawable.ic_launcher; //通知图标
                int iconempty= R.drawable.ic_launcher;
                CharSequence tickerText = "自定义Notification"; //状态栏显示的通知文本提示
                long when = System.currentTimeMillis(); //通知产生的时间，会在通知信息里显示
                //用上面的属性初始化Nofification
                Notification notification4 = new Notification(icon,tickerText,when);
                RemoteViews contentView = new RemoteViews(NotificationActivity.this.getPackageName(), R.layout.mystatus_notifacation_layout);
                contentView.setImageViewResource(R.id.image, iconempty);
                contentView.setTextViewText(R.id.text, "自定义Notification显示内容");
                notification4.contentView = contentView;
           //     Intent m_Intent = new Intent(this,Main.class);
                PendingIntent contentIntent = PendingIntent.getActivity(this,0,m_Intent,0);
                notification4.contentIntent = contentIntent;
                //把Notification传递给NotificationManager
                m_NotificationManager.notify(4,notification4);

                break;
            case R.id.natification_Button05:
                break;

        }
    }
}

